package com.cskaoyan.service;

import com.cskaoyan.bean.storage.Storage;
import com.cskaoyan.bean.storage.StorageExample;
import com.cskaoyan.bean.storage.StorageListVO;
import com.cskaoyan.mapper.StorageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ObjectStorageServiceImpl implements ObjectStorageService {

    @Autowired
    StorageMapper storageMapper;

    /**
     * 创建一个存储对象
     */
    @SneakyThrows
    @Override
    public Storage create(MultipartFile myFile) {
        Storage storageBean = new Storage();
        String originalFilename = myFile.getOriginalFilename();
        long size = myFile.getSize();
        //Long length = myFile.getBytes().length;
        String contentType = myFile.getContentType();
        int i = contentType.lastIndexOf("/");
        String type = contentType.substring(i + 1);
//        long size = myFile.getSize();
        String string = UUID.randomUUID().toString().replaceAll("-","");
        File file = new File("D:\\mall", string + "." + type);
        String url = "http://localhost:8083/pic/" + string + "." + type;
        myFile.transferTo(file);
        storageBean.setSize(size);
        storageBean.setAddTime(new Date());
        storageBean.setKey(string);
        storageBean.setName(originalFilename);
        storageBean.setType(contentType);
        storageBean.setUrl(url);
        storageBean.setUpdateTime(new Date());
        storageMapper.insertSelective(storageBean);
        storageBean = storageMapper.selectByKey(string);
        return storageBean;
    }

    /**
     *返回全部存储对象
     */
    @Override
    public StorageListVO list(Integer page, Integer limit, String sort, String order, String key, String name) {
        PageHelper.startPage(page,limit);
        StorageExample storageExample = new StorageExample();
        StorageExample.Criteria criteria = storageExample.createCriteria();
        if (!StringUtils.isEmpty(key)) {
            criteria.andKeyEqualTo(key);
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        storageExample.setOrderByClause(sort + " " + order);
        List<Storage> storages = storageMapper.selectByExample(storageExample);
        PageInfo pageInfo = new PageInfo(storages);
        long total = pageInfo.getTotal();
        StorageListVO storageListVO = new StorageListVO();
        storageListVO.setItems(storages);
        storageListVO.setTotal(total);
        return storageListVO;
    }

    /**
     * 更新存储对象
     */
    @Override
    public Storage update(Storage recetive) {
        recetive.setUpdateTime(new Date());
        storageMapper.updateByPrimaryKeySelective(recetive);
        Storage storage = storageMapper.selectByPrimaryKey(recetive.getId());
        return storage;
    }

    /**
     * 删除存储对象
     * @param storage
     */
    @Override
    public void delete(Storage storage) {
        storageMapper.deleteByPrimaryKey(storage.getId());
    }


}
